{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "64cd739d",
   "metadata": {},
   "source": [
    "可以直接作用于for循环的数据类型：\n",
    "1.集合数据类型：list，tuple，dict，set，str\n",
    "2.generator，包括生成器和带yield的generator function"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efa80f26",
   "metadata": {},
   "source": [
    "这些可以直接作用于for循环的对象统称为可迭代对象：Iterable"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5de42225",
   "metadata": {},
   "source": [
    "可以使用isinstance()判断一个对象是否是Iterable对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "78542d76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections.abc import Iterable\n",
    "isinstance([],Iterable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dd49cd6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance((x for x in range(10)), Iterable) #生成器"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23c6932b",
   "metadata": {},
   "source": [
    "可以被next函数调用并不断返回下一个值的对象称为迭代器：Iterator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "89332e4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections.abc import Iterator\n",
    "isinstance([],Iterator)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f76cedad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance((x for x in range(10)), Iterator)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d32b274d",
   "metadata": {},
   "source": [
    "把list, dict, str等Iterable变成Iterator可以使用iter函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "33e85634",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(iter([]), Iterator)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e116e286",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(iter('abd'), Iterator)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "208dd09f",
   "metadata": {},
   "source": [
    "Python的Iterator表示一个数据流，Iterator对象可以被next函数调用并不断返回下一个数据，直到没有数据时抛出Stopiteration错误。\n",
    "可以把这个数据流看作是一个有序序列，但我们却不能提前知道序列的长度，只能通过调用next获取下一个数据。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "601762db",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84fdbdc0",
   "metadata": {},
   "source": [
    "凡是可作用于for循环的对象都是Iterable类型：\n",
    "凡是可作用域next函数的对象都是Iterator，表示一个惯性序列：\n",
    "Python的for循环，本质上就是通过不断调用next函数实现的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "070312fb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
